package ciicos.service.impl;

import ciicos.entity.*;
import ciicos.mapper.LineMaintainMapper;
import ciicos.service.*;
import ciicos.util.relation.SubRealtion;
import ciicos.util.relation.WrapperRelation;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ciicos.util.RandomUtil;

import java.math.BigDecimal;

import cn.hutool.core.util.ObjectUtil;
import ciicos.permission.model.authorize.AuthorizeConditionModel;

import ciicos.util.GeneraterSwapUtil;
import ciicos.model.visiual.superQuery.ConditionJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryConditionModel;


import ciicos.permission.service.AuthorizeService;


import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;

import java.util.stream.Collectors;

import ciicos.model.linemaintain.*;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.SimpleDateFormat;

import ciicos.util.*;

import java.util.*;

import ciicos.base.model.filter.RuleInfo;
import ciicos.permission.util.GenUtil;

/**
 * 管线维护管理
 * 版本： V3.2.0
 * 版权：
 * 作者： 企业研发中心
 * 日期： 2025-10-06
 */
@Service
public class LineMaintainServiceImpl extends ServiceImpl<LineMaintainMapper, LineMaintainEntity> implements LineMaintainService {

    @Autowired
    private LineMaintainRecordService lineMaintainRecordService;

    private final static Class eClass = LineMaintainEntity.class;

    @Override
    public List<LineMaintainEntity> getList(LineMaintainPagination pagination) {
        String dataType = pagination.getDataType();
        QueryWrapper<LineMaintainEntity> queryWrapper = new QueryWrapper<>();
        WrapperRelation wrapperRelation = new WrapperRelation(eClass);
        wrapperRelation.getSubRealtionList().add(new SubRealtion(lineMaintainRecordService,LineMaintainRecordEntity.class,"id","maintainid"));
        AutoCodeUtil.highQuery(pagination.getSuperQueryJson(),queryWrapper,wrapperRelation);

        if (ObjectUtil.isNotEmpty(pagination.getName())) {
            queryWrapper.lambda().like(LineMaintainEntity::getName, pagination.getName());
        }

        if (ObjectUtil.isNotEmpty(pagination.getLineplacetype())) {
            queryWrapper.lambda().eq(LineMaintainEntity::getLineplacetype, pagination.getLineplacetype());
            if (ObjectUtil.isNotEmpty(pagination.getLineplaceid())) {
                queryWrapper.lambda().eq(LineMaintainEntity::getLineplaceid, pagination.getLineplaceid());
            }
        }


        //排序+分页
        return AutoCodeUtil.sortPageList(this,queryWrapper, eClass,
                pagination.getSidx(), pagination.getSort(),
                LineMaintainEntity::getId, true,pagination,StringUtil.equals("0",dataType));

    }

    @Override
    public LineMaintainEntity getInfo(String id) {
        QueryWrapper<LineMaintainEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LineMaintainEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(LineMaintainEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, LineMaintainEntity entity) {
        return this.updateById(entity);
    }

    @Override
    public void delete(LineMaintainEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    @Override
    public List<LineMaintainRecordEntity> getLineMaintainRecordList(String parentId) {
        QueryWrapper<LineMaintainRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LineMaintainRecordEntity::getMaintainid, parentId);
        return lineMaintainRecordService.list(queryWrapper);
    }

    @Override
    public List<LineMaintainRecordEntity> getLineMaintainRecordListByIdList(List<String> parentIdList) {
        if(ListUtil.isEmpty(parentIdList)){
            return new ArrayList<>();
        }
        QueryWrapper<LineMaintainRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(LineMaintainRecordEntity::getMaintainid, parentIdList);
        return lineMaintainRecordService.list(queryWrapper);
    }

    //验证表单唯一字段
    @Override
    public String checkForm(LineMaintainForm form, int i) {
        int total = 0;
        boolean isUp = StringUtil.isNotEmpty(form.getId()) && !form.getId().equals("0");
        String id = "";
        String countRecover = "";
        if (isUp) {
            id = form.getId();
        }
        if (form.getLinemaintainrecordList() != null) {
        }

        return countRecover;
    }


}
